import java.io.*;
import java.net.Inet4Address;
import java.net.InetSocketAddress;
import java.net.Socket;

public class Client {

    public static void main(String[] args) throws IOException {
        Socket socket = new Socket();

        socket.setSoTimeout(3000);
        //连接本地 端口 超时
        socket.connect(new InetSocketAddress(Inet4Address.getLocalHost(), 2000), 3000);

        System.out.println("客户端onLine...");
        System.out.println("客户端：" + socket.getLocalAddress() + " 本地端口:" + socket.getLocalPort());
        System.out.println("服务器：" + socket.getInetAddress() + " 服务器端口：" + socket.getPort());
        try {
            todo(socket);
        } catch (Exception e) {
            System.out.println("客户端异常关闭");
        }
        socket.close();
        System.out.println("客户端已退出");

    }

    private static void todo(Socket socket) throws IOException {
        InputStream inputStream = System.in;
        BufferedReader inputReader = new BufferedReader(new InputStreamReader(inputStream));

        //客户端的输出
        OutputStream outputStream = socket.getOutputStream();
        PrintStream printStream = new PrintStream(outputStream);


        InputStream inputStream1 = socket.getInputStream();
        BufferedReader inputReaderout = new BufferedReader(new InputStreamReader(inputStream1));

        boolean flg=true;
        do {
            //键盘输入
            String red = inputReader.readLine();
            printStream.println(red);

            String in = inputReaderout.readLine();
            if ("bye".equalsIgnoreCase(in)){
                flg=false;
            }else {
                System.out.println(in);
            }
        }while (flg);
        printStream.close();
        inputReaderout.close();
    }
}
